package br.com.danieljr.crowdmap;

import java.util.List;

import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import br.com.danieljr.crowdmap.fabricas.FabricaPontos;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;

public class MainActivity extends FragmentActivity {

	// private AlarmeColeta alarme;
	private GoogleMap mapa;
	Location l;
	List<CircleOptions> lista;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
		if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		} else {
			l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		}
		lm = null;
		mapa = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map)).getMap();
		mapa.setMyLocationEnabled(true);
		mapa.animateCamera(CameraUpdateFactory.zoomTo(15));
		mapa.setOnCameraChangeListener(new OnCameraChangeListener() {

			@Override
			public void onCameraChange(CameraPosition arg0) {
				renderCirculos();
			}
		});
	}

	private void adicionaCirculos() {
		// TODO Auto-generated method stub
		lista = FabricaPontos.retornaPontos(this);
		renderCirculos();
	}

	private void renderCirculos() {
		for (CircleOptions circulo : lista) {
			if (estaVisivel(circulo))
				mapa.addCircle(circulo);
		}
	}

	private boolean estaVisivel(CircleOptions c) {
		LatLng centro = c.getCenter();
		LatLngBounds l = mapa.getProjection().getVisibleRegion().latLngBounds;
		if (l.contains(centro))
			return true;
		return false;
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		if (l != null) {
			LatLng c = new LatLng(l.getLatitude(), l.getLongitude());
			mapa.moveCamera(CameraUpdateFactory.newLatLngZoom(c, 15));
		}
		adicionaCirculos();
		super.onStart();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.config) {
			Intent configuracao = new Intent(this, ConfiguracaoActivity.class);
			startActivity(configuracao);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
